﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Role>>" %>
<%@ Import Namespace="Iesi.Collections" %>
<%@ Import Namespace="Ornament.MVCWebFrame.Models" %>
<%@ Import Namespace="System.Collections.ObjectModel" %>
<%@ Import Namespace="Iesi.Collections.Generic" %>
<%@ Import Namespace="Ornament.MemberShip" %>
<%@ Import Namespace="Ornament.Web" %>
<%@ Import Namespace="System.Linq" %>
<script type="text/C#" runat="server">

    private Dictionary<Role, bool> Roles = new Dictionary<Role, bool>();
    private HashedSet<Guid> existPermisions = new HashedSet<Guid>();

    protected void Page_Load(object sender, EventArgs e)
    {
        foreach (var role in OrnamentContext.Current.GetDaoFactory<IMemberShipFactory>().CreateRoleDao().GetAll())
        {
            Roles.Add(role, false);
        }

        if (this.Model != null)
        {
            foreach (Role role in Model)
            {
                var ids = from p in role.Permissions select p.Id;
                existPermisions.AddAll(ids.ToArray<Guid>());
                Roles[role] = true;
            }
        }

    }

    private static string GetPermissionComment(Role role)
    {
        var result = new string[role.Permissions.Count + 1];
        result[0] = role.Remark;
        int idx = 1;

        foreach (var p in role.Permissions)
        {
            result[idx] = p.Name;
            idx++;
        }

        return String.Join(@"&#13;&#10;", result);
    }
    private static string PermissionMap(IEnumerable<Role> roles)
    {
        var s = new StringBuilder("{");
        int roleIndex = 0;
        foreach (Role role in roles)
        {
            if (roleIndex != 0)
            {
                s.Append(",\r\n\t");
            }
            s.Append(role.Name).Append(":").Append("[");
            roleIndex++;
            var pIndex = 0;
            foreach (var p in role.Permissions)
            {
                if (pIndex != 0)
                {
                    s.Append(",\r\n\t\t");
                }
                pIndex++;
                s.Append("\"").Append(p.Id).Append("\"");
            }
            s.Append("]");
        }
        s.Append("}");
        return s.ToString();
    }

</script>
<script src="/Scripts/Lib/jquery.doubleSelectBox.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">

    var permissions=<%=PermissionMap(Roles.Keys)%>    

    var iniRoleAssing = false;
    $(document).ready(function() {
        if (!iniRoleAssing) {                        
            $(".rolePanel input[type=checkbox]").change(roleCheckBox_click).each(roleCheckBox_click)          
            
        }
    });    
    
    function roleCheckBox_click()
    {
        $(".permissionPanel div").removeClass("highlight");
        $(".rolePanel input:checked").each(function(){
            var permissionList=permissions[$(this).attr("id")];
            $.each(permissionList,function(){
                $(".permissionPanel div[id="+this+"]").addClass("highlight");
            });
        });
    } 
</script>
<style type="text/css">
    .rolePanel
    {
        width: 450px;
        border-right: solid 1px #000;
        margin: 5px;
        float: left;
        height: 300px;
        overflow: auto;
    }
    .permissionPanel
    {
        margin: 5px;
        float: left;
    }
    .highlight
    {
        color: Blue;
    }
    h4
    {
        font-weight: bold;
    }
</style>
<div class="rolePanel">
    <h3>
        角色</h3>
    <%
        foreach (var role in Roles.Keys)
        {
    %>
    <input name="roles" id="<%=role.Name %>" type="checkbox" value="<%=role.Name %>"
        <%=this.Roles[role]?"checked='checked'":""%> />
    <label for="<%=role.Name %>" title="<%=GetPermissionComment(role) %>">
        <%=role.Name %>
    </label>
    <%
                
        } 
    %>
</div>
<div class="permissionPanel">
    <h3>
        许可证</h3>
    <%
        var permissions = from a in OrnamentContext.Current.GetDaoFactory<IMemberShipFactory>().Permissions
                          orderby a.Name
                          select a;
        Guid resourceId = Guid.Empty;
        foreach (var bag in permissions)
        {
            if (bag.Resource.Id != resourceId)
            {
                Response.Write(String.Format("<h4>{0}</h4>", bag.Resource.Name));
                Response.Write("<div>");
            }
            Response.Write(String.Format("<div id=\"{0}\">{1}</div>", bag.Id, bag.Name));
            if (bag.Resource.Id != resourceId)
            {
                Response.Write("</div>");
                resourceId = bag.Resource.Id;
            }
        }
                          
    %>
</div>
<div style="clear: both">
</div>
